package com.gzhxhx.czh.myhuibaomin.Activity;

import android.graphics.Color;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzhxhx.czh.myhuibaomin.R;
import com.gzhxhx.czh.myhuibaomin.fragment.FenleiFragment;
import com.gzhxhx.czh.myhuibaomin.fragment.GerenFeagment;
import com.gzhxhx.czh.myhuibaomin.fragment.ShequFragment;
import com.gzhxhx.czh.myhuibaomin.fragment.ShoyeFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import static com.gzhxhx.czh.myhuibaomin.httpapi.HttpApi.ICON;

@ContentView(R.layout.activity_home)
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.ll_home_shoye)
    private LinearLayout llShoye;
    @ViewInject(R.id.ll_home_fenlei)
    private LinearLayout llFenlei;
    @ViewInject(R.id.ll_home_shequ)
    private LinearLayout llShequ;
    @ViewInject(R.id.ll_home_geren)
    private LinearLayout llGeren;
    @ViewInject(R.id.img_home_shoye)
    private ImageView imgShoye;
    @ViewInject(R.id.img_home_fenlei)
    private ImageView imgFenlei;
    @ViewInject(R.id.img_home_shequ)
    private ImageView imgShequ;
    @ViewInject(R.id.img_home_geren)
    private ImageView imgGeren;
    @ViewInject(R.id.tv_home_shoye)
    private TextView tvShoye;
    @ViewInject(R.id.tv_home_fenlei)
    private TextView tvFenlei;
    @ViewInject(R.id.tv_home_geren)
    private TextView tvGeren;
    @ViewInject(R.id.tv_home_shequ)
    private TextView tvShequ;

    private ShoyeFragment shoyeFragment;
    private FenleiFragment fenleiFragment;
    private ShequFragment shequFragment;
    private GerenFeagment gerenFeagment;

    private String shoye,fenlei,shequ,geren
            ,shoyeimg,fenleiimg,shequimg,gerenimg
            ,shoyeimg1,fenleiimg1,shequimg1,gerenimg1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        httpIcon();
        intView();

    }

    private void httpIcon() {
        OkGo.<String>get(ICON)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.d("TAG",response.body());
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            JSONObject object = jsonArray.getJSONObject(0);
                            shoye = object.optString("iconname");
                            shoyeimg = object.optString("icon");
                            shoyeimg1 = object.optString("icon1");
                            JSONObject object2 = jsonArray.getJSONObject(1);
                            fenlei = object2.optString("iconname");
                            fenleiimg = object2.optString("icon");
                            fenleiimg1 = object2.optString("icon1");
                            JSONObject object3 = jsonArray.getJSONObject(2);
                            shequ = object3.optString("iconname");
                            shequimg = object3.optString("icon");
                            shequimg1 = object3.optString("icon1");
                            JSONObject object4 = jsonArray.getJSONObject(3);
                            geren = object4.optString("iconname");
                            gerenimg = object4.optString("icon");
                            gerenimg1 = object4.optString("icon1");
                            Picasso.with(HomeActivity.this)
                                    .load(shoyeimg1)
                                    .error(R.drawable.logo)
                                    .into(imgShoye);
                            Picasso.with(HomeActivity.this)
                                    .load(fenleiimg)
                                    .error(R.drawable.logo)
                                    .into(imgFenlei);
                            Picasso.with(HomeActivity.this)
                                    .load(shequimg)
                                    .error(R.drawable.logo)
                                    .into(imgShequ);
                            Picasso.with(HomeActivity.this)
                                    .load(gerenimg)
                                    .error(R.drawable.logo)
                                    .into(imgGeren);
                            tvShoye.setText(shoye);
                            tvFenlei.setText(fenlei);
                            tvShequ.setText(shequ);
                            tvGeren.setText(geren);

                        } catch (JSONException e) {
                            Log.d("TAG","解析错误");
                            e.printStackTrace();
                        }
                    }
                });


    }

    private void intView() {
        llShoye.setOnClickListener(this);
        llFenlei.setOnClickListener(this);
        llShequ.setOnClickListener(this);
        llGeren.setOnClickListener(this);
        clearSelection();
        shoyefragment();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_home_shoye:
                clearSelection();
                shoyefragment();
                break;
            case R.id.ll_home_fenlei:
                clearSelection();
                fenLeifragment();
                break;
            case R.id.ll_home_shequ:
                clearSelection();
                sheQufragment();
                break;
            case R.id.ll_home_geren:
                clearSelection();
                geRenfragment();
                break;
        }
    }

    private void geRenfragment() {
// 实例化Fragment页面
        gerenFeagment = new GerenFeagment();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_home_content, gerenFeagment);
        // 事务管理提交
        fragmentTransaction.commit();
//        atIv.setImageResource(R.drawable.message);
        tvGeren.setText(geren);
        Picasso.with(this)
                .load(gerenimg1)
                .error(R.drawable.logo)
                .into(imgGeren);
        tvGeren.setTextColor(Color.parseColor("#ff0000"));


        // 改变选中状态
        // 改变选中状态
        tvShoye.setSelected(false);
        imgShoye.setSelected(false);
        tvFenlei.setSelected(false);
        imgFenlei.setSelected(false);
        tvShequ.setSelected(false);
        imgShoye.setSelected(false);
        tvGeren.setSelected(true);
        imgGeren.setSelected(true);

    }

    private void sheQufragment() {
// 实例化Fragment页面
        shequFragment = new ShequFragment();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_home_content, shequFragment);
        // 事务管理提交
        fragmentTransaction.commit();
//        atIv.setImageResource(R.drawable.message);
        tvShequ.setText(shequ);
        Picasso.with(this)
                .load(shequimg1)
                .error(R.drawable.logo)
                .into(imgShequ);
        tvShequ.setTextColor(Color.parseColor("#ff0000"));


        // 改变选中状态
        // 改变选中状态
        tvShoye.setSelected(false);
        imgShoye.setSelected(false);
        tvFenlei.setSelected(false);
        imgFenlei.setSelected(false);
        tvShequ.setSelected(true);
        imgShoye.setSelected(true);
        tvGeren.setSelected(false);
        imgGeren.setSelected(false);


    }

    private void fenLeifragment() {
        // 实例化Fragment页面
        fenleiFragment = new FenleiFragment();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_home_content, fenleiFragment);
        // 事务管理提交
        fragmentTransaction.commit();
//        atIv.setImageResource(R.drawable.message);
        tvFenlei.setText(fenlei);
        Picasso.with(this)
                .load(fenleiimg1)
                .error(R.drawable.logo)
                .into(imgFenlei);
        tvFenlei.setTextColor(Color.parseColor("#ff0000"));


        // 改变选中状态
        // 改变选中状态
        tvShoye.setSelected(false);
        imgShoye.setSelected(false);
        tvFenlei.setSelected(true);
        imgFenlei.setSelected(true);
        tvShequ.setSelected(false);
        imgShoye.setSelected(false);
        tvGeren.setSelected(false);
        imgGeren.setSelected(false);

    }

    private void shoyefragment() {
        // 实例化Fragment页面
        shoyeFragment = new ShoyeFragment();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_home_content, shoyeFragment);
        // 事务管理提交
        fragmentTransaction.commit();
//        atIv.setImageResource(R.drawable.message);
        tvShoye.setText(shoye);
        Picasso.with(this)
                .load(shoyeimg1)
                .error(R.drawable.logo)
                .into(imgShoye);
        tvShoye.setTextColor(Color.parseColor("#ff0000"));


        // 改变选中状态
        // 改变选中状态
        tvShoye.setSelected(true);
        imgShoye.setSelected(true);
        tvFenlei.setSelected(false);
        imgFenlei.setSelected(false);
        tvShequ.setSelected(false);
        imgShoye.setSelected(false);
        tvGeren.setSelected(false);
        imgGeren.setSelected(false);

    }

    private void clearSelection() {
        Picasso.with(this)
                .load(shoyeimg)
                .error(R.drawable.logo)
                .into(imgShoye);
        Picasso.with(this)
                .load(fenleiimg)
                .error(R.drawable.logo)
                .into(imgFenlei);
        Picasso.with(this)
                .load(shequimg)
                .error(R.drawable.logo)
                .into(imgShequ);
        Picasso.with(this)
                .load(gerenimg)
                .error(R.drawable.logo)
                .into(imgGeren);
        tvShoye .setTextColor(Color.parseColor("#666666"));
        tvFenlei.setTextColor(Color.parseColor("#666666"));
        tvShequ.setTextColor(Color.parseColor("#666666"));
        tvGeren.setTextColor(Color.parseColor("#666666"));

    }

}
